package com.gdit.zax.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.gdit.zax.Constant;
import com.gdit.zax.dao.GroupDao;
import com.gdit.zax.entitys.Group;
import com.gdit.zax.entitys.UserInfo;
import com.gdit.zax.factory.DAOFactory;
import com.gdit.zax.utils.DBUtil;
import com.gdit.zax.utils.O;
import com.gdit.zax.utils.TextUtil;

public class GetGradeInfo extends HttpServlet {

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		this.doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		int cid = TextUtil.String2int(request.getParameter("cid"), -1);
		
		
		//检测参数
		if (cid < 0) {
			O.printFail(response, 1, "请检查id参数!");
			return;
		}
		
		//检测连接、用户
		Connection conn = DBUtil.getConnection();
		
		if (conn==null) {
			O.printFail(response, 1000, "数据库连接失败!");
			return;
		}
		
		//开始
		DAOFactory dao = DAOFactory.init(conn, request, response);
		GroupDao gdao = dao.getGroupDao();
		Group ginfo = gdao.getGroupInfo(cid);
		
		if (ginfo
				==null)
		{
			O.printFail(response, 1, "改班级不存在!");
			return;
		}
		Map<String, Object> data = new HashMap<String, Object>();
		data.put("classname", ginfo.getName());
		data.put("pic",Constant.WEBRoot + ginfo.getPic());
		data.put("calssmembersum", gdao.getGroupSubSum(cid));
		UserInfo uinfo = gdao.getGroupTeacherInfo(cid);
		if (uinfo==null) {
			data.put("teachername", "未知");
		} else {
			data.put("teachername", uinfo.getNick());
		}
		data.put("icourse", -1);
		data.put("ranking", gdao.getGroupRanking(cid));
		data.put("discussion", -1);
		
		O.printSuccess(response, data);
	}
}
